//
//  CenterLoopView.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/28.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CenterLoopView.h"
#import "CenterThreeCell.h"
@interface CenterLoopView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)NSMutableArray *imageArra;
@end

@implementation CenterLoopView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self getMiddleLatestMatchInformationWithShowcurrentpage:@"10"];
        [self createCollectionUI];
    }
    return self;
}

/**
 创建collectioview的布局
 */
-(void)createCollectionUI{
    self.collectionView.scrollsToTop = YES;
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.flowLayout.minimumLineSpacing = 9;
    self.flowLayout.minimumInteritemSpacing = 0;
    [self.collectionView setShowsHorizontalScrollIndicator:NO];
    self.flowLayout.itemSize = CGSizeMake(scaleLeftAndRight(240), scaleTopAndBottom(150));
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.frame.size.height) collectionViewLayout:self.flowLayout];
    [self addSubview:self.collectionView];
    [self.collectionView registerClass:[CenterThreeCell class] forCellWithReuseIdentifier:@"CenterThreeCell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArra.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CenterThreeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CenterThreeCell" forIndexPath:indexPath];
    cell.model = self.imageArra[indexPath.row];
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 10, 0, 10);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MatchListModel *model = self.imageArra[indexPath.row];
    self.valueBlock(model);
    NSLog(@"点击了哦");
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    scrollView.showsHorizontalScrollIndicator = NO;
}
/**
 获取最新赛事
 */
-(void)getMiddleLatestMatchInformationWithShowcurrentpage:(NSString *)currentpage{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = ServerIp@"/matchApp/newMatchList";
    NSDictionary *param = @{@"currentPage":@"1",@"showCount":currentpage,@"type":@"true"};
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *dataArra = jsonDic[@"matchList"];
        for (NSDictionary *dict in dataArra) {
            MatchListModel *model = [MatchListModel modelWithDictionary:dict];
            [self.imageArra addObject:model];
        }
        [self.collectionView reloadData];
        NSLog(@"%@",dataArra);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@",error);
    }];
    
}
-(NSMutableArray *)imageArra{
    if (_imageArra == nil) {
        _imageArra = [[NSMutableArray alloc] init];
    }
    return _imageArra;
}
@end
